import { _decorator, Component, Node, v3, Tween, tween } from 'cc';
import { GlobalConfig } from '../../../Init/Config/GlobalConfig';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { BasicEffect } from './BasicEffect';
const { ccclass, property } = _decorator;

@ccclass('GuideArrow')
export class GuideArrow extends BasicEffect {
    follow2dNode: Node = null;
    isFinish = false;
    animPos = v3();

    init(follow2dNode: Node): void {
        this.follow2dNode = follow2dNode;
        this.setPos();
        this.showAnim();
    }
    showAnim() {
        tween(this.animPos).repeatForever(
            tween(this.animPos).
                to(0.5, { x: -1 }, { easing: 'sineIn' }).
                to(0.5, { x: 0 }, { easing: 'sineOut' }).start()
        ).start();
    }

    reset(): void {
        Tween.stopAllByTarget(this.animPos);
    }

    update(dt: any): void {
        if (!this.follow2dNode || this.follow2dNode && !this.follow2dNode.active) {
            GlobalPool.put(this.node);
            this.isFinish = true;
            return;
        }

        this.setPos();
    }

    tmpPos = v3();
    setPos() {
        this.tmpPos.x = this.follow2dNode.position.x * GlobalConfig.Scale2DTo3D;
        this.tmpPos.z = -this.follow2dNode.position.y * GlobalConfig.Scale2DTo3D;
        this.tmpPos.y = 0;
        this.tmpPos.add(this.animPos);
        this.node.setPosition(this.tmpPos);
    }

}

